package org.jeecg.modules.jd.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Joiner;
import org.jeecg.modules.jd.entity.JD_position;
import org.jeecg.modules.jd.service.IJD_positionService;
import org.jeecg.modules.jd.util.JDPositionUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class JDPositionTask implements Job {

    @Autowired
    private IJD_positionService jD_positionService;


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status", 0);
        List<JD_position> jd_positionList = jD_positionService.list(queryWrapper);
        int count = jD_positionService.count();
        int init_num = 0;
        if (null != jd_positionList) {
            init_num = jd_positionList.size();
        }
        if (init_num < 50) {
            int pageIndex = count / 100 + 1;
            String positionStr = JDPositionUtil.getPosition(pageIndex, 100);
            JSONObject positionJsonObject = JSON.parseObject(positionStr);
            String positionDataJson = positionJsonObject.getString("data");
            JSONObject positionDataJsonObject = JSON.parseObject(positionDataJson);
            Integer total = positionDataJsonObject.getInteger("total");
            if (count < total) {
                String contentlist = positionDataJsonObject.getString("result");
                List<JD_position> old_jd_positionList = JSON.parseArray(contentlist, JD_position.class);
                for (JD_position jd_position : old_jd_positionList) {

                    if (null == jD_positionService.getById(jd_position.getId())) {
                        jd_position.setStatus(0);
                        jD_positionService.save(jd_position);
                    }
                }
                init_num = total + 2;
            }
            List<String> spaceNameList = new ArrayList<>();
            for (int i = init_num; i < 50; i++) {
                spaceNameList.add("神奇口袋" + (count + i));
            }
            List<JD_position> jd_positionList1 = new ArrayList<>();
            String resultData = JDPositionUtil.createPosition(Joiner.on(",").join(spaceNameList));
            JSONObject jsonObject = JSON.parseObject(JSON.parseObject(resultData).getString("data"));
            Long unionId = jsonObject.getLong("unionId");
            Long siteId = jsonObject.getLong("siteId");
            Long type = jsonObject.getLong("type");
            String resultList = jsonObject.getString("resultList");
            JSONObject jsonObject1 = JSON.parseObject(resultList);
            Set<Map.Entry<String, Object>> entrySet = jsonObject1.entrySet();
            //将关系集合entrySet进行迭代，存放到迭代器中
            Iterator<Map.Entry<String, Object>> it2 = entrySet.iterator();
            while (it2.hasNext()) {
                //获取Map.Entry关系对象me
                Map.Entry<String, Object> me = it2.next();
                //通过关系对象获取key
                String spaceName = me.getKey();
                //通过关系对象获取value
                Long id = (Long) me.getValue();
                JD_position jd_position = new JD_position();
                jd_position.setId(id);
                jd_position.setSiteId(siteId);
                jd_position.setType(type);
                jd_position.setUnionId(unionId);
                jd_position.setSpaceName(spaceName);
                jd_position.setStatus(0);
                jd_positionList1.add(jd_position);
            }

            jD_positionService.saveBatch(jd_positionList1);

        }

    }
}
